//
//  NSString+extension.m
//  CurriculumFactory
//
//  Created by apple on 2020/4/2.
//

#import "NSString+extension.h"

//#import <AppKit/AppKit.h>


@implementation NSString (extension)
//是否是有用的 不等 @“” 不等于 nil
- (BOOL)isHelpful {
    if ([self isEqualToString:@""] || self == nil) {
        return NO;
    }
    return YES;
}
#pragma mark 手机号验证
/// 手机号验证
/// @param phone 手机号
+ (BOOL)verifyPhoneNumber:(NSString*)phone {
    phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (phone.length != 11)
    {
        return NO;
    }else{
        /**
         * 移动号段正则表达式
         */
        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
        /**
         * 联通号段正则表达式
         */
        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
        /**
         * 电信号段正则表达式
         */
        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
        BOOL isMatch1 = [pred1 evaluateWithObject:phone];
        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
        BOOL isMatch2 = [pred2 evaluateWithObject:phone];
        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
        BOOL isMatch3 = [pred3 evaluateWithObject:phone];
        
        if (isMatch1 || isMatch2 || isMatch3) {
            return YES;
        }else{
            return NO;
        }
    }
}


#pragma mark 判断字符串是否含有汉字
- (BOOL)includeChinese {
    for(int i=0; i< [self length];i++) {
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}
#pragma mark 去掉所有的空格及字符串
- (NSString *)removeSpaceAndNewline:(NSString *)str{
     NSMutableString *mutStr = [NSMutableString stringWithString:str];

        NSRange range = {0,mutStr.length};

        [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];

        NSRange range2 = {0,mutStr.length};

        [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];

        return mutStr;

}
#pragma mark 考级吗验证 只能输入大小字母 和数字
- (BOOL)is_test_code {
    BOOL result =NO;
    NSString *regex = @"[A-Za-z0-9]+";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    result = [pred evaluateWithObject:self];
    return result;
}
#pragma mark 字符串只能是字母和数字
- (BOOL)validation_code {
    BOOL result =NO;
    NSString *regex = @"[A-Za-z0-9]+";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    result = [pred evaluateWithObject:self];
    return result;
}

#pragma mark 护照验证
- (BOOL)passport_validation  {
     BOOL result =NO;
       NSString *regex = @"[A-Z0-9]+";
       NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
       result = [pred evaluateWithObject:self];
    return result;
}
#pragma mark 是否是数字
- (BOOL)is_digital {
    BOOL result =NO;
    NSString *regex = @"[0-9]";
   NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
   result = [pred evaluateWithObject:self];
    return result;
}
#pragma mark 是否是纯中文
- (BOOL)is_Chinese {
    BOOL result =NO;
   NSString *regex = @"[\u4e00-\u9fa5]+";
  NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
  result = [pred evaluateWithObject:self];
   return result;
}
#pragma mark 是否是姓名格式
- (BOOL)is_name {
    BOOL result =NO;
    
    NSString *regex = @"[\u4e00-\u9fa5A-Za-z➋-➒]+";
   NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
   result = [pred evaluateWithObject:self];
   return result;
}
#pragma mark 从身份证号中提取出生年月日
- (NSString *)form_idCard_get_birthday {
    NSString * birthday =  @"";
    if (self.length > 16) {
       // 410926 19940124 121X
        NSString * years = [self substringWithRange:NSMakeRange(6, 4)];//出生年
         years  = [NSString stringWithFormat:@"%@.",years];
        NSString * month = [self substringWithRange:NSMakeRange(10, 2)];//出生月
        month  = [NSString stringWithFormat:@"%@.",month];
        
        NSString * day = [self substringWithRange:NSMakeRange(12, 2)];//出生日
        birthday = [NSString stringWithFormat:@"%@%@%@",years,month,day];
        NSLog(@"生日 %@",birthday);
    }
    return birthday;
}


/**
 校验身份证号码是否正确 返回BOOL值
 @return 返回BOOL值 YES or NO
 */
- (BOOL)cly_verifyIDCardString {
    NSString *regex = @"^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    BOOL isRe = [predicate evaluateWithObject:self];
    if (!isRe) {
         //身份证号码格式不对
        return NO;
    }
    //加权因子 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2
    NSArray *weightingArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
    //校验码 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2
    NSArray *verificationArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
    
    NSInteger sum = 0;//保存前17位各自乖以加权因子后的总和
    for (int i = 0; i < weightingArray.count; i++) {//将前17位数字和加权因子相乘的结果相加
        NSString *subStr = [self substringWithRange:NSMakeRange(i, 1)];
        sum += [subStr integerValue] * [weightingArray[i] integerValue];
    }
    
    NSInteger modNum = sum % 11;//总和除以11取余
    NSString *idCardMod = verificationArray[modNum]; //根据余数取出校验码
    NSString *idCardLast = [self.uppercaseString substringFromIndex:17]; //获取身份证最后一位
    
    if (modNum == 2) {//等于2时 idCardMod为10  身份证最后一位用X表示10
        idCardMod = @"X";
    }
    if ([idCardLast isEqualToString:idCardMod]) { //身份证号码验证成功
        return YES;
    } else { //身份证号码验证失败
        return NO;
    }
}
@end
